package com.fs.groupSeven.service.impl;

import com.fs.groupSeven.Dao.StudentDao;
import com.fs.groupSeven.pojo.*;
import com.fs.groupSeven.pojo.Class;
import com.fs.groupSeven.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: 周鑫
 * @createDate: 2020/9/29/029
 * @version: 1.0
 */
//IOC容器帮我们去new这个实现对象
@Service
public class StudentServiceImpl implements StudentService {

    private StudentDao studentDao;

    @Autowired
    public StudentServiceImpl(StudentDao studentDao) {
        this.studentDao = studentDao;
    }

    @Override
    public List<Class> queryClassList(Integer collegeId) {
        List<Class> classes = studentDao.queryClassList(collegeId);
        return classes;
    }

    @Override
    public PageInfo<Student> getStudentsByPage(Integer currentPage, Integer pageSize) {
        //准备拦截mybatis后续sql的查询(加limit关键字)
        //PageHelper.startPage(page,rows)分页查询
        //PageHelper.startPage(page,rows)两个参数，第一个参数是页数。第二个参数是条数，每页查询的条数。(参数的顺序不可颠倒)
        PageHelper.startPage(currentPage,pageSize);
        List<Student> studentList = studentDao.queryStudentByPage();
        //PageHelper内部计算总共有多少页
        PageInfo<Student> studentPageInfo = new PageInfo<>(studentList);
        return studentPageInfo;
    }

    //添加一个学生信息
    @Override
    public boolean addOne(Student student) {
        int i = studentDao.addOne(student);
        return i > 0;
    }

    //编辑一个学生信息
    @Override
    public boolean modifyOne(Student student) {
        int result = studentDao.updateStudent(student);
        return result > 0;
    }

    @Override
    public Student getOneByStudentId(Integer studentId) {
        Student student = studentDao.getOneById(studentId);
        return student;
    }

    @Override
    public int delStudentByStudentId(Integer studentId) {
        int i = studentDao.delStudentByStudentId(studentId);
        return i;
    }

    @Override
    public List<Student> searchStudentsByPage(Integer currentPage, Integer pageSize, String searchValue) {
        //开始查询的序号
        Integer startNum = (currentPage-1) * pageSize;
        List<Student> studentList = studentDao.searchStudentsByPage(startNum,pageSize,searchValue);
        return studentList;
    }

    @Override
    public List<Student> searchStudentsByAll(String searchValue) {
        List<Student> studentList = studentDao.searchStudentsByAll(searchValue);
        return studentList;
    }

    @Override
    public PageInfo<Student> selStudentsByClassId(Integer currentPage, Integer pageSize, Integer classId) {
        PageHelper.startPage(currentPage,pageSize);
        List<Student> studentList = studentDao.queryStudentsByClassId(classId);
        //PageHelper内部计算总共有多少页
        PageInfo<Student> studentPageInfo = new PageInfo<>(studentList);
        return studentPageInfo;
    }



    //根据学号查询学生id，姓名及班级id
    @Override
    public List<Student> queryStudentSomeInformationByStuNum(Integer stuNum) {
        List<Student> info = studentDao.queryStudentSomeInformationByStuNum(stuNum);
        return info;
    }

    //根据gradeId和科目名称判断成绩是否已经存在
    @Override
    public boolean queryScoreIsExistByStudentIdAndSubjectName(String subjectName, Integer studentId) {
        //根据学生id和科目名称判断成绩是否已经存在
        Student gradeId = studentDao.queryGradeIdByStudentIdAndSubjectName(subjectName,studentId);
        if (gradeId != null){//成绩已经存在
            return true;
        }else {
            return false;//不存在
        }
    }

    //添加成绩
    @Override
    public int addGrade(Integer studentId, String subjectName, Integer usualscore, Integer examscore, Integer finalscore) {
        //int addGrade = studentDao.addGrade(studentId,subjectName,usualscore,examscore,finalscore);
        Grade grade = new Grade();
        grade.setStudentId(studentId);
        grade.setSubjectName(subjectName);
        grade.setUsualScore(usualscore);
        grade.setExamScore(examscore);
        grade.setFinalScore(finalscore);
        int addGrade = studentDao.addGradeByGrade(grade);
        return addGrade;
    }


    @Override
    public PageInfo<Grade> getGradeTable(Integer currentPage, Integer pageSize,Integer classId, String subjectName) {
        PageHelper.startPage(currentPage,pageSize);
        List<Grade> studentList = studentDao.queryGradeTable(classId,subjectName);
        //PageHelper内部计算总共有多少页
        PageInfo<Grade> studentPageInfo = new PageInfo<>(studentList);
        return studentPageInfo;
    }

    @Override
    public Student getOneGradeByGradeId(Integer gradeId) {
        Student student = studentDao.getOneGradeByGradeId(gradeId);
        return student;
    }

    @Override
    public int editGradeByGradeId(Integer gradeId, Integer usualscore, Integer examscore, Integer finalscore) {
        int i = studentDao.editGradeByGradeId(gradeId,usualscore,examscore,finalscore);
        return i;
    }

    @Override
    public List<Student> searchStudentGrade(String searchValue) {
        List<Student> studentGradeList = studentDao.searchStudentGradeByAll(searchValue);
        return studentGradeList;
    }

    @Override
    public List<Class> showClassesByIndex() {
        List<Class> classList = studentDao.showClassesByIndex();
        return classList;
    }

    @Override
    public List<Subject> querySubjectList(Integer collegeId) {
        List<Subject> subjectList = studentDao.querySubjectList(collegeId);
        return subjectList;
    }

    @Override
    public List<College> selCollegeNameList() {
        List<College> subjectList = studentDao.selCollegeNameByCollegeId();
        return subjectList;
    }
}
